home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / uucp-104.lha / uucp-1.04 / copy.c < prev    next >
C/C++ Source or Header  |  1993-02-13  |  4KB  |  203 lines

  1. /* copy.c
  2.    Copy one file to another for the UUCP package.
  3.  
  4.    Copyright (C) 1991, 1992 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Infinity Development Systems, P.O. Box 520, Waltham, MA 02254.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #if USE_RCS_ID
  29. const char copy_rcsid[] = "$Id: copy.c,v 1.12 1992/11/14 16:10:27 ian Rel $";
  30. #endif
  31.  
  32. #include "uudefs.h"
  33. #include "system.h"
  34. #include "sysdep.h"
  35.  
  36. #include <stdio.h>
  37. #include <errno.h>
  38.  
  39. /* Copy one file to another.  */
  40.  
  41. #if USE_STDIO
  42.  
  43. boolean
  44. fcopy_file (zfrom, zto, fpublic, fmkdirs)
  45.      const char *zfrom;
  46.      const char *zto;
  47.      boolean fpublic;
  48.      boolean fmkdirs;
  49. {
  50.   FILE *efrom;
  51.   boolean fret;
  52.  
  53.   efrom = fopen (zfrom, BINREAD);
  54.   if (efrom == NULL)
  55.     {
  56.       ulog (LOG_ERROR, "fopen (%s): %s", zfrom, strerror (errno));
  57.       return FALSE;
  58.     }
  59.  
  60.   fret = fcopy_open_file (efrom, zto, fpublic, fmkdirs);
  61.   (void) fclose (efrom);
  62.   return fret;
  63. }
  64.  
  65. boolean
  66. fcopy_open_file (efrom, zto, fpublic, fmkdirs)
  67.      FILE *efrom;
  68.      const char *zto;
  69.      boolean fpublic;
  70.      boolean fmkdirs;
  71. {
  72.   FILE *eto;
  73.   char ab[8192];
  74.   int c;
  75.  
  76.   eto = esysdep_fopen (zto, fpublic, FALSE, fmkdirs);
  77.   if (eto == NULL)
  78.     return FALSE;
  79.  
  80.   while ((c = fread (ab, sizeof (char), sizeof ab, efrom)) != 0)
  81.     {
  82.       if (fwrite (ab, sizeof (char), (size_t) c, eto) != c)
  83.     {
  84.       ulog (LOG_ERROR, "fwrite: %s", strerror (errno));
  85.       (void) fclose (eto);
  86.       (void) remove (zto);
  87.       return FALSE;
  88.     }
  89.     }
  90.  
  91.   if (fclose (eto) != 0)
  92.     {
  93.       ulog (LOG_ERROR, "fclose: %s", strerror (errno));
  94.       (void) remove (zto);
  95.       return FALSE;
  96.     }
  97.  
  98.   return TRUE;
  99. }
  100.  
  101. #else /* ! USE_STDIO */
  102.  
  103. #if HAVE_FCNTL_H
  104. #include <fcntl.h>
  105. #else
  106. #if HAVE_SYS_FILE_H
  107. #include <sys/file.h>
  108. #endif
  109. #endif
  110.  
  111. #ifndef O_RDONLY
  112. #define O_RDONLY 0
  113. #define O_WRONLY 1
  114. #define O_RDWR 2
  115. #endif
  116.  
  117. #ifndef O_NOCTTY
  118. #define O_NOCTTY 0
  119. #endif
  120.  
  121. boolean
  122. fcopy_file (zfrom, zto, fpublic, fmkdirs)
  123.      const char *zfrom;
  124.      const char *zto;
  125.      boolean fpublic;
  126.      boolean fmkdirs;
  127. {
  128.   int ofrom;
  129.   boolean fret;
  130.  
  131.   ofrom = open (zfrom, O_RDONLY | O_NOCTTY, 0);
  132.   if (ofrom < 0)
  133.     {
  134.       ulog (LOG_ERROR, "open (%s): %s", zfrom, strerror (errno));
  135.       return FALSE;
  136.     }
  137.  
  138.   fret = fcopy_open_file (ofrom, zto, fpublic, fmkdirs);
  139.   (void) close (ofrom);
  140.   return fret;
  141. }
  142.  
  143. boolean
  144. fcopy_open_file (ofrom, zto, fpublic, fmkdirs)
  145.      int ofrom;
  146.      const char *zto;
  147.      boolean fpublic;
  148.      boolean fmkdirs;
  149. {
  150.   int oto;
  151.   char ab[8192];
  152.   int c;
  153.  
  154.   /* These file mode arguments are from the UNIX version of sysdep.h;
  155.      each system dependent header file will need their own
  156.      definitions.  */
  157.   oto = creat (zto, fpublic ? IPUBLIC_FILE_MODE : IPRIVATE_FILE_MODE);
  158.   if (oto < 0)
  159.     {
  160.       if (errno == ENOENT && fmkdirs)
  161.     {
  162.       if (! fsysdep_make_dirs (zto, fpublic))
  163.         return FALSE;
  164.       oto = creat (zto,
  165.                fpublic ? IPUBLIC_FILE_MODE : IPRIVATE_FILE_MODE);
  166.     }
  167.       if (oto < 0)
  168.     {
  169.       ulog (LOG_ERROR, "open (%s): %s", zto, strerror (errno));
  170.       return FALSE;
  171.     }
  172.     }
  173.  
  174.   while ((c = read (ofrom, ab, sizeof ab)) > 0)
  175.     {
  176.       if (write (oto, ab, (size_t) c) != c)
  177.     {
  178.       ulog (LOG_ERROR, "write: %s", strerror (errno));
  179.       (void) close (oto);
  180.       (void) remove (zto);
  181.       return FALSE;
  182.     }
  183.     }
  184.  
  185.   if (close (oto) < 0)
  186.     {
  187.       ulog (LOG_ERROR, "close: %s", strerror (errno));
  188.       (void) remove (zto);
  189.       return FALSE;
  190.     }
  191.  
  192.   if (c < 0)
  193.     {
  194.       ulog (LOG_ERROR, "read: %s", strerror (errno));
  195.       (void) remove (zto);
  196.       return FALSE;
  197.     }
  198.  
  199.   return TRUE;
  200. }
  201.  
  202. #endif /* ! USE_STDIO */
  203.